package com.luoye.sh.task;

import com.suweia.utils.lock.ISyncLock;
import com.suweia.utils.lock.ISyncRunner;
import com.suweia.utils.lock.impl.SyncLockByRedis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisKeyValueTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/1/20
 * Time: 11:28
 * 任务初始化
 */
@Configuration
@EnableScheduling
public class TaskInitSchedule implements SchedulingConfigurer {
    @Value("${server.port}")
    String port;

    private static Logger logger = LoggerFactory.getLogger(TaskInitSchedule.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/2 * * * ? ")
    public void orderStatisciticsFirstTime() {
        logger.info("测试定时任务,port->"+port);
//        try {
//            ValueOperations operations=redisTemplate.opsForValue();
//            if(operations.setIfAbsent("lock","lock")){
//                redisTemplate.expire("lock",1, TimeUnit.DAYS);
//
//                logger.info("定时任务执行->"+port);
//
//                //等待1秒释放锁
//                Thread.sleep(3000);
//            }
//        }catch (Exception ex){
//
//        }finally {
//            redisTemplate.delete("lock");
//        }

        ISyncLock syncLock=new SyncLockByRedis(redisTemplate,1,TimeUnit.DAYS);
        try {
            syncLock.execute(new ISyncRunner() {
                public void run() {
                    int i=1/0;
                    logger.info("定时任务执行->"+port);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception ex){
            logger.warn("异常->",ex);
        }



    }

    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ScheduledExecutorService localExecutor = Executors.newScheduledThreadPool(2);
        TaskScheduler taskScheduler = new ConcurrentTaskScheduler(localExecutor);
        taskRegistrar.setTaskScheduler(taskScheduler);
    }

}
